<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends MY_Controller {
    
    var $controller_path 	    = '/dept/index/';
    var $model_path 		    = 'dept/dept_model';
    var $lang_path 		    = 'dept/dept';
	
    function __construct()
    {
		parent::__construct();
		log_message('debug', "dept/index Index controller Class Initialized.");
    }
	
    public function index()
    {
        $map_arr = NULL;
        //$seach_arr = $this->get_input("columnFilters"); //获得数据
        //print_r($seach_arr);
        //$map_arr["label"] = $seach_arr->label->value;

        $dept_arr = $this->dept_model->alls($map_arr);

		$this->api("200",$dept_arr,"");
    }

    public function delete()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $ids = $this->get_input();
        $ids_arr = NULL;
        $type = false;
        if($ids)
        {
            foreach($ids as $val)
            {
                $ids_arr[] = $val->id;
            }
            $type = $this->dept_model->dels($ids_arr);
        }


        if($type) {
            $ret_data['type'] = 200;
            $ret_data['message'] = $this->lang->line('dept_success');
        }
        else {
            $ret_data['message'] = $this->lang->line('dept_error');
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"机构删除：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }

    public function save()
    {
        $ret_data['type'] = 304;
        $ret_data['message'] = '';
        $ret_data['data'] = NULL;

        $post_data = $this->get_input("","array");

        if(!isset($post_data["parentId"])) $post_data["parentId"] = 0 ;

        $this->form_validation->set_data($post_data);
        $this->form_validation->dir_set_rules('dept/dept');
        if($this->form_validation->run() == true) {

            if($post_data["id"]) $data_arr['id'] = $post_data["id"];

            $data_arr['name'] = $post_data["name"];
            $data_arr['order_num'] = $post_data["orderNum"];
            $data_arr['parent_id'] = $post_data["parentId"];

            $type = $this->dept_model->saves($data_arr);
            if($type) {
                $ret_data['type'] = 200;
                $ret_data['data'] = $type;
                $ret_data['message'] = $this->lang->line('dept_success');
            }
            else {
                $ret_data['message'] = $this->lang->line('dept_error');
            }
        }
        else {
            $ret_data['message'] = $this->form_validation->error_string();
        }
        $this->write_logs($ret_data['type'],$ret_data['data'],"机构更新：".$ret_data['message']);
        $this->api($ret_data['type'],$ret_data['data'],$ret_data['message']);
    }
}
